import _extends from 'babel-runtime/helpers/extends';
import _toConsumableArray from 'babel-runtime/helpers/toConsumableArray';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/**
 * 数据字典单选框组件
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
import * as React from 'react';
import { Radio } from 'antd';
var RadioGroup = Radio.Group;

var DictRadioGroup = function (_React$Component) {
    _inherits(DictRadioGroup, _React$Component);

    function DictRadioGroup(props) {
        _classCallCheck(this, DictRadioGroup);

        var _this = _possibleConstructorReturn(this, (DictRadioGroup.__proto__ || Object.getPrototypeOf(DictRadioGroup)).call(this, props));

        _this.getRadioGroup = function () {
            var _this$props = _this.props,
                dictkey = _this$props.dictkey,
                filterkeys = _this$props.filterkeys,
                order = _this$props.order,
                storagetype = _this$props.storagetype;

            var items = void 0;
            if (storagetype && storagetype === "localStorage") {
                items = localStorage['dict_' + dictkey];
            } else {
                items = sessionStorage['dict_' + dictkey];
            }
            if (!items) {
                return '';
            }
            items = JSON.parse(items);
            if (filterkeys) {
                items = items.filter(function (item) {
                    return filterkeys.includes(item.itemKey);
                });
            }
            if (order) {
                var orderItems = order.map(function (itemKey) {
                    return items.filter(function (item) {
                        return item.itemKey === itemKey;
                    })[0];
                });
                orderItems.push.apply(orderItems, _toConsumableArray(items.filter(function (item) {
                    return !order.includes(item.itemKey);
                })));
                items = orderItems;
            }
            var options = items.map(function (item) {
                return {
                    label: item.itemName,
                    value: item.itemKey
                };
            });
            return React.createElement(RadioGroup, _extends({}, _this.props, { options: options, className: 'dict-radio-group' }));
        };
        // 校验
        var dictkey = _this.props.dictkey;

        if (!dictkey) {
            throw new Error('Dict.RadioGroup组件需要属性：dictkey');
        }
        return _this;
    }

    _createClass(DictRadioGroup, [{
        key: 'render',
        value: function render() {
            return this.getRadioGroup();
        }
    }]);

    return DictRadioGroup;
}(React.Component);

export default DictRadioGroup;